var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
var tea_id, tea_sound_synopsis, tea_comment_arr = [],nav_id, tea_comment_totalPage, tea_comment_Num = 1;
Page({
  data: {
    currentIdx: 0,
    flag: true,
    careflag: true,
    httpNativetalkFile: 'http://file.waijiaojun.com/',
    MiddleTitle: ['主页', '一对一', '系统课'],
    commentNum: ['评价(15)'],
    audioPlay: true,
    audioFlag: false,
    systemClassNull: false,
    imgUrl: 'http://file.waijiaojun.com/waijiaojun/icon',
  },
  onLoad: function (e) {
    tea_id = e.teaId;
  },
  onShow: function (e) {
    var that = this;
    wx.request({
      url: httpNativetalk + '/app/8/teacher/detail',
      data: {
        "tea_id": tea_id,
      },
      header: {//请求头
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": '',
      },
      method: 'POST',
      success: function (res) {
        var teaImg = 'http://file.waijiaojun.com/' + res.data.obj.teacher_detail.tea_head_portrait
        console.log(res.data.obj);
        var commentNum = [];
        commentNum.push('评价(' + res.data.obj.comment_size + ')');
        //音频简介地址处理
        tea_sound_synopsis = res.data.obj.teacher_detail.tea_sound_synopsis;
        if (tea_sound_synopsis !== null) {
          var teaSoundAddressTop = tea_sound_synopsis.substring(0, 4);
          tea_sound_synopsis = that.address(teaSoundAddressTop, tea_sound_synopsis);
        }
        //视频简介封面地址处理
        var tea_intro_video_cover = res.data.obj.teacher_detail.tea_intro_video_cover;
        if (tea_intro_video_cover !== null) {
          var addressTop = tea_intro_video_cover.substring(0, 4);
          tea_intro_video_cover = that.address(addressTop, tea_intro_video_cover);
        }
        //视频简介地址处理
        var tea_intro_video = res.data.obj.teacher_detail.tea_intro_video;
        if (tea_intro_video !== null) {
          var videoAddressTop = tea_intro_video.substring(0, 4)
          tea_intro_video = that.address(videoAddressTop, tea_intro_video);
        }
        that.setData({
          tea_sound_synopsis: tea_sound_synopsis,
          tea_intro_video_cover: tea_intro_video_cover,
          tea_intro_video: tea_intro_video,
          teacherInforData: res.data.obj,
          state: res.data.obj.teacher_detail.state,
          commentNum: commentNum,
          teaImg: teaImg,
          tea_id: tea_id
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    });

    // 系统课请求
    wx.request({
      url: httpWeike + '/v4.2/student/find/list/for/teacher',
      data: {
        "currentPage": 1,
        "pageSize": 1000,
        "tea_id": tea_id,
      },
      header: {//请求头
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": '',
      },
      method: 'POST',
      success: function (res) {
        // console.log(res);
        // console.log(res.data.obj.data);
        //var systemClassNull=res.data.obj.data 
        if (res.data.obj.data == '') {
          that.setData({
            systemClassNull: true,
          })
        };
        that.setData({
          systemClass: res.data.obj.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    });
    wx.showShareMenu({
      withShareTicket: true,
    });
    this.tea_commentFun(tea_comment_Num, tea_id);
  }, 
  //评论内容请求
  tea_commentFun: function (tea_comment_Num, tea_id) {
    var that = this;
    wx.request({
      url: httpNativetalk + '/app/10/teacher/detail/comment',
      data: {
        "currentPage": tea_comment_Num,
        "obj": tea_id,
      },
      header: {//请求头
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": '',
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data.obj);
        // var commentsData = res.data.obj;
        var replayData = res.data.obj.comments;
        tea_comment_totalPage = res.data.obj.totalPage;
        if (replayData == null) {
          that.setData({
            commentFlag: true,
          });
        } else {
          tea_comment_arr.push(replayData);
          function getDate(time) {
            var date = new Date(time);
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
            return (Y + M + D);
          };
          console.log(replayData, replayData.length);
          console.log(replayData[0].memb_head_portrait);
          for (var i = 0; i < replayData.length; i++) {
            //时间戳的处理
            replayData[i].com_time = getDate(replayData[i].com_time);
            //评论老师回复
            if (replayData[i].com_reply_content == null || replayData[i].com_reply_content == "") {
              replayData[i].com_reply_content = false;
            };
            //头像连接的处理
            var headImg = replayData[i].memb_head_portrait;

            if (headImg !== null) {
              var headImgAddress = headImg.slice(0, 4);
              if (headImgAddress == "/nat") {
                replayData[i].memb_head_portrait = 'http://file.waijiaojun.com' + replayData[i].memb_head_portrait;
              } else if (headImgAddress == "http") {
                replayData[i].memb_head_portrait = replayData[i].memb_head_portrait;
              } else if (headImgAddress == "waij") {
                replayData[i].memb_head_portrait = 'http://file.waijiaojun.com/' + replayData[i].memb_head_portrait;
              };
            }
          }
          that.setData({
            replayData: replayData,
            tea_comment_arr: tea_comment_arr,
          });
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    });
  },

  //点击语音播放
  audioPlayFun: function (e) {
    var that = this;
    var dataUrl = e.currentTarget.dataset.audio_src;
    console.log(dataUrl)
    that.setData({
      audioFlag: true,
    })
    if (dataUrl == tea_sound_synopsis) {
      console.log("地址一致,播放成功")
      wx.playBackgroundAudio({
        dataUrl: dataUrl,
        success: function (res) {
          console.log(res, '播放成功');
        }
      });
      wx.onBackgroundAudioStop(function () {
        console.log('播放停止')
        that.setData({ audioFlag: false, })
      })
    } else {
      console.log("地址不一致")
    }
    // wx.playBackgroundAudio({
    //   dataUrl: dataUrl,
    // });
    // wx.onBackgroundAudioStop(function () {
    //   that.setData({ audioFlag: false, })
    // })
  },
  //语音暂停
  audioPauseFun: function (e) {
    this.setData({
      audioFlag: false,
    })
    wx.pauseBackgroundAudio();

  },
  //页面隐藏语音停止
  onUnload: function (e) {
    console.log('页面隐藏')
    wx.pauseBackgroundAudio();
  },
  classInforFun: function (e) {
    var classIds = e.currentTarget.dataset.cou_id;
    var titleData = e.currentTarget.dataset.titles;
    wx.navigateTo({
      url: '/pages/classDetail/classDetail?classId=' + classIds,
    });
    wx.setNavigationBarTitle({
      title: titleData,
    })
  },
  clickButton1: function () {
    this.setData({
      flag: false
    })
  },
  clickButton2: function () {
    this.setData({
      flag: true
    })
  },
  aa: function (e) {
    console.log(e)
  },
  //小导航栏
  navFun: function (e) {
    nav_id=e.currentTarget.dataset.idx;
    this.setData({
      currentIdx: e.currentTarget.dataset.idx
    });
  },
  careFun1: function (e) {
    this.setData({
      careflag: false,
    })
  },
  careFun2: function (e) {
    this.setData({
      careflag: true,
    })
  },
  //立即预约
  nowAppiont: function (e) {
    // console.log(e)

    var teaIds = e.currentTarget.dataset.tea_id;
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/bindPhone/bindPhone',
      })
    }
    if (app.globalData.token !== null) {
      //查询是否购买过课程

      wx.navigateTo({
        // url: '/pages/selectTime/selectTime?teaId=' + teaIds + '&date=' + dates + '&classId=' + classId,
        url: '/pages/myClass/myClass?teaId=' + teaIds,
      });
    }
  },
  //跳转系统课详情页面
  systemClassDetailFun: function (e) {
    var weikeIds = e.currentTarget.dataset.weikeid;
    wx.navigateTo({
      url: '/pages/systemClass/systemClass?weikeId=' + weikeIds,
    })
  },
  // 上拉触底加载更多
  onReachBottom: function () {
    if (nav_id==3){
      tea_comment_Num++;
      if (tea_comment_Num > 1 && tea_comment_Num < tea_comment_totalPage) {
        this.tea_commentFun(tea_comment_Num, tea_id);
      };
    }
  },
  //地址处理函数
  address: function (e, f) {
    if (e == null) {
      e = null;
      return e;
    } else if (e == "/nat") {
      e = 'http://file.waijiaojun.com' + f;
      return e;
    } else if (e == "http") {
      e = f;
      return e;
    } else if (e == "waij") {
      e = 'http://file.waijiaojun.com/' + f;
      return e;
    } else if (e == "/wai") {
      e = 'http://file.waijiaojun.com' + f;
      return e;
    };
  }
})